package com.jiancong.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiancong.domain.ResponseResult;
import com.jiancong.domain.entity.Article;
import com.jiancong.domain.entity.Category;
import com.jiancong.domain.vo.CategoryVo;
import com.jiancong.domain.vo.PageVo;
import com.jiancong.enums.AppHttpCodeEnum;
import com.jiancong.exception.SystemException;
import com.jiancong.mapper.CategoryMapper;
import com.jiancong.service.ArticleService;
import com.jiancong.service.CategoryService;
import com.jiancong.utils.BeanCopyUtils;
import com.jiancong.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 分类表(Category)表服务实现类
 *
 * @author makejava
 * @since 2022-02-02 12:29:52
 */
@Service
public class CategoryServiceImpl implements CategoryService{

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<CategoryVo> getCategoryList() {
        //查询文章表  状态为已发布的文章,获取文章的分类id，并且去重 ,查询分类表,封装vo
        return categoryMapper.getCategoryList();
    }

    @Override
    public ResponseResult<Category> selectCategoryPage(Category category, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        //把记录所有数据的集合转化为Page类型
        PageInfo<Category> pageInfo = new PageInfo<>(categoryMapper.CategoryList(category));
        List<Category> tagVos = pageInfo.getList();
        PageVo pageVo = new PageVo(tagVos,pageInfo.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    @Transactional
    public ResponseResult save(Category category) {
        if(!StringUtils.hasText(category.getName()) || !StringUtils.hasText(category.getDescription())){
            throw new SystemException(AppHttpCodeEnum.LABEL_CANNOT);
        }
        if(!StringUtils.hasText(category.getStatus())){
            throw new SystemException(AppHttpCodeEnum.LABEL_CANNOT);
        }

        Long userId = SecurityUtils.getUserId();
        category.setCreateBy(userId);
        category.setUpdateBy(userId);
        category.setCreateTime(new Date());
        category.setUpdateTime(new Date());
        categoryMapper.save(category);

        return ResponseResult.okResult();


    }

    @Override
    @Transactional
    public ResponseResult removeById(Long id) {
        Category category = new Category();
        Long userId = SecurityUtils.getUserId();
        category.setUpdateBy(userId);
        category.setId(id);
        category.setUpdateTime(new Date());
        categoryMapper.removeById(category);
        return ResponseResult.okResult();
    }

    @Override
    public List<Category> list() {
        return categoryMapper.list();
    }

    @Override
    public List<CategoryVo> listAllCategory() {
        return categoryMapper.listAllCategory();
    }

    @Override
    public ResponseResult getCategoryById(Long id) {
        // 根据id查询分类信息
        Category category = categoryMapper.getCategoryById(id);
        if(Objects.isNull(category)){
            throw new SystemException(AppHttpCodeEnum.CATEGORY_NOT_EXIST);
        }
        // 封装vo返回
        CategoryVo categoryVo = BeanCopyUtils.copyBean(category, CategoryVo.class);
        return ResponseResult.okResult(categoryVo);
    }

    @Override
    @Transactional
    public ResponseResult updateCategory(Category category) {
        if(!StringUtils.hasText(category.getName())){
            throw new SystemException(AppHttpCodeEnum.CATEGORY_NAME_NOT_NULL);
        }
        if(!StringUtils.hasText(category.getDescription())){
            throw new SystemException(AppHttpCodeEnum.CATEGORY_DESCRIPTION_NOT_NULL);
        }
        if(!StringUtils.hasText(category.getStatus())){
            throw new SystemException(AppHttpCodeEnum.CATEGORY_STATUS_NOT_NULL);
        }
        
        // 设置更新人和更新时间
        category.setUpdateTime(new Date());
        category.setUpdateBy(SecurityUtils.getUserId());
        
        categoryMapper.updateCategory(category);
        return ResponseResult.okResult();
    }



        //转换成VO
//        List<Category> categories = page.getRecords();

//    @Override
//    public ResponseResult getCategoryList() {
//        //查询文章表  状态为已发布的文章
//        LambdaQueryWrapper<Article> articleWrapper = new LambdaQueryWrapper<>();
//        articleWrapper.eq(Article::getStatus,SystemConstants.ARTICLE_STATUS_NORMAL);
//        List<Article> articleList = articleService.list(articleWrapper);
//        //获取文章的分类id，并且去重
//        Set<Long> categoryIds = articleList.stream()
//                .map(article -> article.getCategoryId())
//                .collect(Collectors.toSet());
//
//        //查询分类表
//        List<Category> categories = listByIds(categoryIds);
//        categories = categories.stream().
//                filter(category -> SystemConstants.STATUS_NORMAL.equals(category.getStatus()))
//                .collect(Collectors.toList());
//        //封装vo
//        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(categories, CategoryVo.class);
//
//        return ResponseResult.okResult(categoryVos);
//    }
//
//    @Override
//    public List<CategoryVo> listAllCategory() {
//        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(Category::getStatus, SystemConstants.NORMAL);
//        List<Category> list = list(wrapper);
//        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(list, CategoryVo.class);
//        return categoryVos;
//    }
//
//    @Override
//    public PageVo selectCategoryPage(Category category, Integer pageNum, Integer pageSize) {
//        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
//
//        queryWrapper.like(StringUtils.hasText(category.getName()),Category::getName, category.getName());
//        queryWrapper.eq(Objects.nonNull(category.getStatus()),Category::getStatus, category.getStatus());
//
//        Page<Category> page = new Page<>();
//        page.setCurrent(pageNum);
//        page.setSize(pageSize);
//        page(page,queryWrapper);
//
//        //转换成VO
//        List<Category> categories = page.getRecords();
//
//        PageVo pageVo = new PageVo();
//        pageVo.setTotal(page.getTotal());
//        pageVo.setRows(categories);
//        return pageVo;
//    }
}

